//
//  ServiceTableViewCell.m
//  NewProject
//
//  Created by admin on 2017/6/14.
//  Copyright © 2017年 李航飞. All rights reserved.
//

#import "ServiceTableViewCell.h"
@interface ServiceTableViewCell ()
@property (nonatomic, strong)UIImageView *timeImageView;
@property (nonatomic, strong)UILabel *timeLabel;
@property (nonatomic, strong)UILabel *detailLabel;
@property (nonatomic, strong)UILabel *tipLabel;
@property (nonatomic, strong)UIImageView *rightImageView;

@property (nonatomic, strong)UIImageView *mergeImageView;
@property (nonatomic, strong)UILabel *mergeLabel;

@property (nonatomic, strong)UIView *bottomView;
@property (nonatomic, strong)UIButton *chooseButton;
@property (nonatomic, strong)UIImageView *goodImageView;
@property (nonatomic, strong)UILabel *goodLabel;
@property (nonatomic, strong)UILabel *priceLabel;
@property (nonatomic, strong)UIButton *minusButton;
@property (nonatomic, strong)UIButton *addButton;
@property (nonatomic, strong)UILabel *numLabel;

@property (nonatomic, strong)UIButton *allChooseButton;
@property (nonatomic, strong)UILabel *allChooseLabel;
@property (nonatomic, strong)UILabel *costLabel;
@property (nonatomic, strong)UIButton *payButton;
@property (nonatomic, strong)UIView *topView;

@end

@implementation ServiceTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.selectionStyle = UITableViewCellSelectionStyleNone;

        if([reuseIdentifier isEqualToString:@"0"]){
            _timeImageView = [[UIImageView alloc]init];
            _timeImageView.image = [UIImage imageNamed:@"时间"];
            [self.contentView addSubview:_timeImageView];

            _timeLabel = [[UILabel alloc]init];
            _timeLabel.font = FONT(15);
            _timeLabel.text = @"收货时间";
            _timeLabel.textColor = RGB0X(0x333333);
            [self.contentView addSubview:_timeLabel];
            
            _detailLabel = [[UILabel alloc]init];
            _detailLabel.font = FONT(12);
            _detailLabel.text = @"¥0起送 不满¥30 收¥5运费";
            _detailLabel.textColor = RGB0X(0x7E7E7E);
            [self.contentView addSubview:_detailLabel];
            
            _tipLabel = [[UILabel alloc]init];
            _tipLabel.font = FONT(14);
            _tipLabel.text = @"可修改";
            _tipLabel.textColor = RGB0X(0xFF9E7D);
            [self.contentView addSubview:_tipLabel];
        
            _rightImageView = [[UIImageView alloc]init];
            _rightImageView.image = [UIImage imageNamed:@"箭头"];
            [self.contentView addSubview:_rightImageView];
            
            _bottomView = [[UIView alloc]init];
            _bottomView.frame = CGRectMake(0, 59/WIDTH_6_SCALE, WIDTH, 1/WIDTH_6_SCALE);
            _bottomView.backgroundColor = RGB0X(0xF8F9F6);
            [self.contentView addSubview:_bottomView];
            
            [self createTopConstraint];

        }else if([reuseIdentifier isEqualToString:@"1"]){
            _mergeImageView = [[UIImageView alloc]init];
            _mergeImageView.image = [UIImage imageNamed:@"凑单"];
            [self.contentView addSubview:_mergeImageView];
            
            _mergeLabel = [[UILabel alloc]init];
            _mergeLabel.font = FONT(15);
            _mergeLabel.text = @"凑单专区";
            _mergeLabel.textColor = RGB0X(0x333333);
            [self.contentView addSubview:_mergeLabel];
            
            _rightImageView = [[UIImageView alloc]init];
            _rightImageView.image = [UIImage imageNamed:@"箭头"];
            [self.contentView addSubview:_rightImageView];
            
            _bottomView = [[UIView alloc]init];
            _bottomView.frame = CGRectMake(0, 51/WIDTH_6_SCALE, WIDTH, 1/WIDTH_6_SCALE);
            _bottomView.backgroundColor = RGB0X(0xF8F9F6);
            [self.contentView addSubview:_bottomView];

            [self createMiddleConstraint];
        }else if([reuseIdentifier isEqualToString:@"last"]){
            _allChooseButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [_allChooseButton setImage:[UIImage imageNamed:@"未选中"] forState:UIControlStateNormal];
            [_allChooseButton addTarget:self action:@selector(allButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            [_allChooseButton setImage:[UIImage imageNamed:@"选中"] forState:UIControlStateSelected];
            [self.contentView addSubview:_allChooseButton];
            
            _allChooseLabel = [[UILabel alloc]init];
            _allChooseLabel.font = FONT(13);
            _allChooseLabel.text = @"全选";
            _allChooseLabel.textColor = RGB0X(0x333333);
            [self.contentView addSubview:_allChooseLabel];
            
            _costLabel = [[UILabel alloc]init];
            _costLabel.font = FONT(13);
            _costLabel.text = @"共¥36.5";
            _costLabel.textColor = RGB0X(0xFFAA6C);
            [self.contentView addSubview:_costLabel];
            
            _payButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [_payButton setBackgroundColor:RGB0X(0xFFD601)];
            [_payButton setTitle:@"结算" forState:UIControlStateNormal];
            _payButton.titleLabel.font = FONT(15);
            _payButton.titleLabel.textColor = [UIColor blackColor];
            [_payButton addTarget:self action:@selector(payButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.contentView addSubview:_payButton];
            
            _topView = [[UIView alloc]init];
            _topView.frame = CGRectMake(0, 0, WIDTH, 8/WIDTH_6_SCALE);
            _topView.backgroundColor = RGB0X(0xF8F9F6);
            [self.contentView addSubview:_topView];
            
            _bottomView = [[UIView alloc]init];
            _bottomView.frame = CGRectMake(0, 57/WIDTH_6_SCALE, WIDTH, 1/WIDTH_6_SCALE);
            _bottomView.backgroundColor = RGB0X(0xF8F9F6);
            [self.contentView addSubview:_bottomView];

            [self createLastConstraint];
        }else{
            _chooseButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [_chooseButton setImage:[UIImage imageNamed:@"未选中"] forState:UIControlStateNormal];
            [_chooseButton addTarget:self action:@selector(chooseButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            [_chooseButton setImage:[UIImage imageNamed:@"选中"] forState:UIControlStateSelected];
            _chooseButton.tag = [reuseIdentifier integerValue];
            [self.contentView addSubview:_chooseButton];
            
            _goodImageView = [[UIImageView alloc]init];
            [self.contentView addSubview:_goodImageView];
            
            _goodLabel = [[UILabel alloc]init];
            _goodLabel.font = FONT(12);
            _goodLabel.textColor = RGB0X(0x7E7E7E);
            [self.contentView addSubview:_goodLabel];
            
            _priceLabel = [[UILabel alloc]init];
            _priceLabel.font = FONT(16);
            _priceLabel.textColor = RGB0X(0x7E7E7E);
            [self.contentView addSubview:_priceLabel];

            _minusButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [_minusButton setImage:[UIImage imageNamed:@"减"] forState:UIControlStateNormal];
            [_minusButton addTarget:self action:@selector(minusButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.contentView addSubview:_minusButton];

            _addButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [_addButton setImage:[UIImage imageNamed:@"加"] forState:UIControlStateNormal];
            [_addButton addTarget:self action:@selector(addButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.contentView addSubview:_addButton];

            _numLabel = [[UILabel alloc]init];
            _numLabel.font = FONT(15);
            _numLabel.textColor = RGB0X(0x333333);
            _numLabel.text = @"1";
            _numLabel.textAlignment = NSTextAlignmentCenter;
            [self.contentView addSubview:_numLabel];

            _rightImageView = [[UIImageView alloc]init];
            _rightImageView.image = [UIImage imageNamed:@"箭头"];
            [self.contentView addSubview:_rightImageView];
            
            _bottomView = [[UIView alloc]init];
            _bottomView.frame = CGRectMake(15/WIDTH_6_SCALE, 82/WIDTH_6_SCALE, WIDTH-30/WIDTH_6_SCALE, 1/WIDTH_6_SCALE);
            _bottomView.backgroundColor = RGB0X(0xF8F9F6);
            [self.contentView addSubview:_bottomView];

            [self createConstraint];
        }
        
    }
    return self;
}

- (void)createTopConstraint{
    [_timeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(15/WIDTH_6_SCALE);
        make.top.equalTo(self.contentView.mas_top).offset(10/WIDTH_6_SCALE);
        make.size.mas_equalTo(CGSizeMake(18/WIDTH_6_SCALE,16/WIDTH_6_SCALE));
    }];
    [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.timeImageView.mas_right).offset(7/WIDTH_6_SCALE);
        make.top.equalTo(self.contentView.mas_top).offset(11/WIDTH_6_SCALE);
    }];
    [_detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(15/WIDTH_6_SCALE);
        make.top.equalTo(self.timeLabel.mas_bottom).offset(10/WIDTH_6_SCALE);
    }];
    [_tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView.mas_right).offset(-30/WIDTH_6_SCALE);
        make.centerY.equalTo(self.contentView.mas_centerY).offset(0);
    }];
    [_rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView.mas_right).offset(-15/WIDTH_6_SCALE);
        make.centerY.equalTo(self.contentView.mas_centerY).offset(0);
    }];
}

- (void)createMiddleConstraint{
    [_mergeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(15/WIDTH_6_SCALE);
        make.centerY.equalTo(self.contentView.mas_centerY).offset(0);
        make.size.mas_equalTo(CGSizeMake(18/WIDTH_6_SCALE,18/WIDTH_6_SCALE));
    }];
    [_mergeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mergeImageView.mas_right).offset(7/WIDTH_6_SCALE);
        make.centerY.equalTo(self.contentView.mas_centerY).offset(0);
    }];
    [_rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView.mas_right).offset(-15/WIDTH_6_SCALE);
        make.centerY.equalTo(self.contentView.mas_centerY).offset(0);
    }];
}

- (void)createConstraint{
    [_chooseButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(15/WIDTH_6_SCALE);
        make.centerY.equalTo(self.contentView.mas_centerY).offset(0);
        make.size.mas_equalTo(CGSizeMake(19/WIDTH_6_SCALE, 19/WIDTH_6_SCALE));
    }];
    [_goodImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.chooseButton.mas_right).offset(10/WIDTH_6_SCALE);
        make.centerY.equalTo(self.contentView.mas_centerY).offset(0);
        make.size.mas_equalTo(CGSizeMake(70/WIDTH_6_SCALE, 70/WIDTH_6_SCALE));
    }];
    [_goodLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.goodImageView.mas_right).offset(18/WIDTH_6_SCALE);
        make.top.equalTo(self.contentView.mas_top).offset(10/WIDTH_6_SCALE);
    }];
    [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.goodLabel.mas_left).offset(0);
        make.top.equalTo(self.goodLabel.mas_bottom).offset(24/WIDTH_6_SCALE);
    }];
    [_minusButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView.mas_right).offset(-80/WIDTH_6_SCALE);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-15/WIDTH_6_SCALE);
        make.size.mas_equalTo(CGSizeMake(25/WIDTH_6_SCALE, 25/WIDTH_6_SCALE));
    }];
    [_addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView.mas_right).offset(-15/WIDTH_6_SCALE);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-15/WIDTH_6_SCALE);
        make.size.mas_equalTo(CGSizeMake(25/WIDTH_6_SCALE, 25/WIDTH_6_SCALE));
    }];
    [_numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.minusButton.mas_right).offset(0);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-20/WIDTH_6_SCALE);
        make.right.equalTo(self.addButton.mas_left).offset(0);
    }];
}

- (void)createLastConstraint{
    [_allChooseButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(15/WIDTH_6_SCALE);
        make.centerY.equalTo(self.contentView.mas_centerY).offset(2/WIDTH_6_SCALE);
        make.size.mas_equalTo(CGSizeMake(19/WIDTH_6_SCALE, 19/WIDTH_6_SCALE));
    }];
    [_allChooseLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.allChooseButton.mas_right).offset(23/WIDTH_6_SCALE);
        make.centerY.equalTo(self.contentView.mas_centerY).offset(2/WIDTH_6_SCALE);
    }];
    [_costLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.allChooseLabel.mas_right).offset(27/WIDTH_6_SCALE);
        make.centerY.equalTo(self.contentView.mas_centerY).offset(2/WIDTH_6_SCALE);
    }];
    [_payButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView.mas_right).offset(0);
        make.top.equalTo(self.topView.mas_bottom).offset(0);
        make.size.mas_equalTo(CGSizeMake(80/WIDTH_6_SCALE, 50/WIDTH_6_SCALE));
    }];
}

- (void)setModel:(NSDictionary *)model{
    [_goodImageView sd_setImageWithURL:[NSURL URLWithString:SF(@"%@%@",IMAGEURL,model[kImage])] placeholderImage:[UIImage imageNamed:@""]];
    _goodLabel.text = model[kTitle];
    _priceLabel.text = SF(@"¥%@",model[kPrice]);
}

- (void)allButtonClick:(UIButton* )sender{
    if (sender.selected == NO) {
        sender.selected = YES;
    }else{
        sender.selected = NO;
    }
}

- (void)payButtonClick:(id )sender{
    if (self.delegate) {
        if ([self.delegate respondsToSelector:@selector(Settlement)]) {
            [self.delegate Settlement];
        }
    }
}

- (void)minusButtonClick:(id )sender{
    NSInteger lastNum = [_numLabel.text integerValue];
    if (lastNum==1) {
        
    }else{
        lastNum--;
    }
    _numLabel.text = SF(@"%ld",lastNum);
}

- (void)addButtonClick:(id )sender{
    NSInteger lastNum = [_numLabel.text integerValue];
    lastNum++;
    _numLabel.text = SF(@"%ld",lastNum);
}

- (void)chooseButtonClick:(UIButton* )sender{
    sender.selected = !sender.selected;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
